package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.shopping.dao.GoodsDao;
import com.yc.shopping.dao.MemberDao;
import com.yc.shopping.util.ShoppingUtil;

import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class SaleMainUI {

	protected Shell shell;
	private Text text_oid;
	private Text txt_aname;
	private Text text_mid;
	private Table table;
	private Text text_gname;
	private Text text_num;
	private Text text__saleprice;
	private Text text_total;
	private Text text_gid;
	GoodsDao goodsDao = new GoodsDao();
	MemberDao memberDao =new MemberDao();
	private List<Map<String,Object>> carts = null;
	private List<Map<String,Object>> member = null;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			SaleMainUI window = new SaleMainUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		/**
		 * 登入判断
		 * 如果未登录则跳转到登陆界面
		 * 
		 */
		if(null ==ShoppingUtil.adminLogin) {
			//跳转到登录界面
			LoginUI ui =new LoginUI();
			shell.dispose();
			ui.open();
		}
		shell.setText("源陈收银系统: "+ShoppingUtil.adminLogin.get("ANAME")+" - "+ShoppingUtil.adminLogin.get("ATYPE"));
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		shell.setSize(1009, 815);
		//shell.setText("收银员系统界面");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		//shell.setMaximized(true);
		ShoppingUtil.centerWindows(shell);
		SashForm sashForm = new SashForm(shell, SWT.VERTICAL);
		
		Composite composite = new Composite(sashForm, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblNewLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		lblNewLabel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel.setBounds(10, 10, 115, 23);
		lblNewLabel.setText("流水号(订单号)：");
		
		text_oid = new Text(composite, SWT.READ_ONLY);
		text_oid.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		text_oid.setText("YC123");
		text_oid.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_oid.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		text_oid.setBounds(129, 10, 190, 29);
		
		txt_aname = new Text(composite, SWT.READ_ONLY);
		txt_aname.setText(ShoppingUtil.objToString(ShoppingUtil.adminLogin.get("ANAME")));
		txt_aname.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txt_aname.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		txt_aname.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		txt_aname.setBounds(451, 10, 99, 29);
		
		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setText("操作员：");
		lblNewLabel_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblNewLabel_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		lblNewLabel_1.setBounds(374, 10, 71, 23);
		
		text_mid = new Text(composite, SWT.READ_ONLY);
		text_mid.setText("无");
		text_mid.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_mid.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		text_mid.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		text_mid.setBounds(679, 10, 128, 29);
		
		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setText("会员号：");
		lblNewLabel_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		lblNewLabel_2.setBounds(595, 10, 61, 23);
		
		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		SashForm sashForm_1 = new SashForm(composite_1, SWT.NONE);
		
		Composite composite_3 = new Composite(sashForm_1, SWT.NONE);
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		
		table = new Table(composite_3, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(20, 0, 654, 475);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(137);
		tblclmnNewColumn.setText("商品编号");
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(166);
		tableColumn.setText("商品条形码");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(113);
		tableColumn_1.setText("名称");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(74);
		tableColumn_2.setText("数量");
		
		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(71);
		tableColumn_3.setText("单价");
		
		TableColumn tableColumn_4 = new TableColumn(table, SWT.NONE);
		tableColumn_4.setWidth(87);
		tableColumn_4.setText("金额");
		
		text_gname = new Text(composite_3, SWT.READ_ONLY);
		text_gname.setForeground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
		text_gname.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		text_gname.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_gname.setBounds(134, 550, 114, 23);
		
		Label lblNewLabel_1_1 = new Label(composite_3, SWT.NONE);
		lblNewLabel_1_1.setText("商品名称");
		lblNewLabel_1_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblNewLabel_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_1_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		lblNewLabel_1_1.setBounds(43, 550, 71, 23);
		
		text_num = new Text(composite_3, SWT.READ_ONLY);
		text_num.setForeground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
		text_num.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		text_num.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_num.setBounds(134, 595, 114, 23);
		
		Label lblNewLabel_1_1_1 = new Label(composite_3, SWT.NONE);
		lblNewLabel_1_1_1.setText("购买数量");
		lblNewLabel_1_1_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblNewLabel_1_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_1_1_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		lblNewLabel_1_1_1.setBounds(43, 595, 71, 23);
		
		text__saleprice = new Text(composite_3, SWT.READ_ONLY);
		text__saleprice.setForeground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
		text__saleprice.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		text__saleprice.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text__saleprice.setBounds(382, 550, 114, 23);
		
		Label lblNewLabel_1_1_2 = new Label(composite_3, SWT.NONE);
		lblNewLabel_1_1_2.setText("商品单价");
		lblNewLabel_1_1_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblNewLabel_1_1_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_1_1_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		lblNewLabel_1_1_2.setBounds(291, 550, 71, 23);
		
		text_total = new Text(composite_3, SWT.READ_ONLY);
		text_total.setForeground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
		text_total.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		text_total.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_total.setBounds(382, 595, 114, 23);
		
		Label lblNewLabel_1_1_3 = new Label(composite_3, SWT.NONE);
		lblNewLabel_1_1_3.setText("商品金额");
		lblNewLabel_1_1_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblNewLabel_1_1_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_1_1_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		lblNewLabel_1_1_3.setBounds(291, 595, 71, 23);
		
		Composite composite_4 = new Composite(sashForm_1, SWT.NONE);
		composite_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		
		Button button_2 = new Button(composite_4, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
				LoginUI ui =new LoginUI();
				ui.open();
			}
		});
		button_2.setBounds(50, 570, 80, 27);
		button_2.setText("登出");
		sashForm_1.setWeights(new int[] {654, 151});
		
		Composite composite_2 = new Composite(sashForm, SWT.NONE);
		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		
		text_gid = new Text(composite_2, SWT.NONE);
		text_gid.setForeground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
		text_gid.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		text_gid.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_gid.setBounds(158, 22, 114, 23);
		
		Label lblNewLabel_1_1_4 = new Label(composite_2, SWT.NONE);
		lblNewLabel_1_1_4.setText("商品");
		lblNewLabel_1_1_4.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblNewLabel_1_1_4.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_1_1_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		lblNewLabel_1_1_4.setBounds(67, 22, 71, 23);
		
		Button btn_pay = new Button(composite_2, SWT.NONE);
		btn_pay.setBounds(341, 18, 80, 27);
		btn_pay.setText("结账 F5");
		
		Button btn_update = new Button(composite_2, SWT.NONE);
		btn_update.setBounds(459, 18, 80, 27);
		btn_update.setText("修改数量 F6");
		
		Button btn_exit = new Button(composite_2, SWT.NONE);
		btn_exit.setBounds(582, 18, 80, 27);
		btn_exit.setText("退货");
		
		Button button = new Button(composite_2, SWT.NONE);
		button.setBounds(698, 18, 80, 27);
		button.setText("报表统计");
		
		Button button_1 = new Button(composite_2, SWT.NONE);
		button_1.setBounds(801, 18, 80, 27);
		button_1.setText("系统设置");
		sashForm.setWeights(new int[] {40, 400, 50});
		//生成流水号
		text_oid.setText(ShoppingUtil.genOrderId());
		
		
		
		/////////////////////////////////
		//添加全局快捷键
		Display.getCurrent().addFilter(SWT.KeyDown,new Listener() {

			@Override
			public void handleEvent(Event e) {
				
				if(e.keyCode==SWT.F5) {
					payOrder();
				}else if(e.keyCode==SWT.F6) {
					updateNum();
				}else if(e.keyCode==13) {
					addCart();
				}
			}
		});
	}

	/**
	 * 结算
	 */
	private void payOrder() {
		//输入会员
		//弹框输入数量
		InputDialog id = new InputDialog(shell,"会员卡","请输入会员卡号","无",null);
		id.open();
		String num = id.getValue();
		//查看会员是否存在
		try {
			member = memberDao.selectOneMember(num);
			if("无".equals(num)||num==null) {
				text_mid.setText(num);//设置会员卡
				
				//计算
				if(null == carts) {
					ShoppingUtil.showMessage(shell, "提示", "购物车为空");
					return;
				}
				int count = 0;//商品总数量
				for (Map<String, Object> map : carts) {			
					int buy_num = Integer.parseInt(ShoppingUtil.objToString(map.get("num")));
					float total = Float.parseFloat(ShoppingUtil.objToString(map.get("total")));
					count+=buy_num;
					ShoppingUtil.totalPrice+=total;		
				}	
				//弹窗
				Dialog_payOrder dp = new Dialog_payOrder(shell,SWT.CLOSE|SWT.APPLICATION_MODAL);
				dp.open();
				//TODO 会员卡积分 修改会员表积分
				int currScore=(int) (ShoppingUtil.totalPrice/10);
				try {
					memberDao.updateScore(num, currScore);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}else if(member!=null){

				text_mid.setText(num);//设置会员卡	
				//计算
				if(null == carts) {
					ShoppingUtil.showMessage(shell, "提示", "购物车为空");
					return;
				}
				int count = 0;//商品总数量
				for (Map<String, Object> map : carts) {			
					int buy_num = Integer.parseInt(ShoppingUtil.objToString(map.get("num")));
					float total = Float.parseFloat(ShoppingUtil.objToString(map.get("total")));
					count+=buy_num;
					ShoppingUtil.totalPrice+=total;		
				}	
				//弹窗
				Dialog_memberPay dp = new Dialog_memberPay(shell,SWT.CLOSE|SWT.APPLICATION_MODAL);
				dp.open();
				//TODO 会员卡积分 修改会员表积分
				int currScore=(int) (ShoppingUtil.totalPrice/10);
				try {
					memberDao.updateScore(num, currScore);
				} catch (SQLException e) {
					e.printStackTrace();
				}		
			}else {
				ShoppingUtil.showMessage(shell, "提示", "查无此会员");
				return;
			}		
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		text_mid.setText(num);//设置会员卡	
		//支付完成 清空购物车表数据	生成一个新的流水帐号
		text_oid.setText(ShoppingUtil.genOrderId());
		carts=null;
		ShoppingUtil.totalPrice=0;
		text_mid.setText("无");
		table.removeAll();
		//清空只读文本框
		text_gname.setText("");
		text_num.setText("");
		text__saleprice.setText("");
		text_total.setText("");
		
		
		
	}
	/*
	 * 修改数量
	 */
	private void updateNum() {
		//获取code内容
		String code = text_gid.getText().trim();
		System.out.println(code);
		//根据文本框中输入的编号查询商品信息
		try {
			Map<String,Object > map =goodsDao.findByIdOrCode(code);
			if(null==map || map.isEmpty()) {
				ShoppingUtil.showMessage(shell, "消息提示", "暂无此商品");
				text_gid.setText("");
				return;
			}
			
			
			InputDialog id =new InputDialog(shell,"商品数量","请输入购买数量","1",null);
			id.open();
			String num = id.getValue();//获取输入的值
			
			//库存判断
			int balance = Integer.parseInt(ShoppingUtil.objToString(map.get("BALANCE")));
			if(balance<=Integer.parseInt(num)) {
				ShoppingUtil.showMessage(shell, "消息提示", "库存不足");
				text_gid.setText("");
				return;
			}
			
			float total = Integer.parseInt(num)*Float.parseFloat(ShoppingUtil.objToString(map.get("SALEPRICE")));
			
			//判断购物车是否为空
			if(carts == null) {
				carts=new ArrayList<Map<String,Object>>();
			}
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("gid", map.get("GID"));
			m.put("gcode", map.get("GCODE"));
			m.put("gname", map.get("GNAME"));
			m.put("saleprice", map.get("SALEPRICE"));
			m.put("num", num);
			m.put("total", total);//当前总金额
			carts.add(m);
			
			//设置文本框
			text_gname.setText(ShoppingUtil.objToString(m.get("gname")));
			text__saleprice.setText(ShoppingUtil.objToString(m.get("saleprice")));
			text_num.setText(ShoppingUtil.objToString(m.get("num")));
			text_total.setText(ShoppingUtil.objToString(m.get("total")));
			text_gid.setText("");
			//显示购物车中所有的数据
			showCart();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*
	 * 添加购物车
	 */
	private void addCart() {
		//获取code内容
		String code = text_gid.getText().trim();
		//根据文本框中输入的编号查询商品信息
		try {
			Map<String,Object > map =goodsDao.findByIdOrCode(code);
			if(null==map || map.isEmpty()) {
				ShoppingUtil.showMessage(shell, "消息提示", "暂无此商品");
				text_gid.setText("");
				return;
			}

			//库存判断
			int balance = Integer.parseInt(ShoppingUtil.objToString(map.get("BALANCE")));
			if(balance<=0) {
				ShoppingUtil.showMessage(shell, "消息提示", "库存不足");
				text_gid.setText("");
				return;
			}
			
			//判断购物车是否为空
			if(carts == null) {
				carts=new ArrayList<Map<String,Object>>();
			}
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("gid", map.get("GID"));
			m.put("gcode", map.get("GCODE"));
			m.put("gname", map.get("GNAME"));
			m.put("saleprice", map.get("SALEPRICE"));
			m.put("num", 1);
			m.put("total", map.get("SALEPRICE"));//当前总金额
			carts.add(m);
			text_gid.setText("");
			//设置文本框
			text_gname.setText(ShoppingUtil.objToString(m.get("gname")));
			text__saleprice.setText(ShoppingUtil.objToString(m.get("saleprice")));
			text_num.setText(ShoppingUtil.objToString(m.get("num")));
			text_total.setText(ShoppingUtil.objToString(m.get("total")));
			
			//显示购物车中所有的数据
			showCart();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void showCart() {
		table.removeAll();
		if(carts==null) {
			return;
		}
		TableItem item = null;
		for(Map<String,Object> m : carts) {
			item = new TableItem(table,SWT.NONE);
			item.setText(new String[] {
					ShoppingUtil.objToString(m.get("gid")),
					ShoppingUtil.objToString(m.get("gcode")),
					ShoppingUtil.objToString(m.get("gname")),
					ShoppingUtil.objToString(m.get("saleprice")),
					ShoppingUtil.objToString(m.get("num")),
					ShoppingUtil.objToString(m.get("total"))
			});
		}
	}
}
